//
//  JaneHomePageController.m
//  EPA
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 张静. All rights reserved.
//

#import "JaneHomePageController.h"
#import "CK_MenuViewController.h"
#import "JaneNetWorkRequestManager.h"
#import "JaneCollectionViewCell.h"
#import "JaneCycleView.h"
#import "JaneHomeDetailController.h"
#import "JaneRadioController.h"
#import "JaneGProductController.h"
#import "JaneTopicController.h"
#import "JaneHomePageController.h"
@interface JaneHomePageController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
//collectionView
@property (nonatomic,strong) UICollectionView *collectionView;
//数组
@property (nonatomic,strong) NSMutableArray *dataSource;
//轮播图属性
@property (nonatomic,strong) JaneCycleView *cycleView;

@end

@implementation JaneHomePageController
//
- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title {
    NSLog(@"第%ld项:%@",(long)index,title);
    [self.navigationController popViewControllerAnimated:YES];
    switch (index) {
            case 1:
        {
            JaneTopicController *topicVC = [[JaneTopicController alloc]init];
            [self.navigationController pushViewController:topicVC animated:YES];
        }
            break;
            case 2:
        {
            JaneRadioController *radioVC = [[JaneRadioController alloc]init];
            [self.navigationController pushViewController:radioVC animated:YES];
        }
            break;
            case 3:
        {
            JaneGProductController *GProductVC = [[JaneGProductController alloc]init];
            [self.navigationController pushViewController:GProductVC animated:YES];
        }
            break;
        case 4:
        {
            JaneHomePageController *detailVC = [[JaneHomePageController alloc]init];
            [self.navigationController pushViewController:detailVC animated:YES];
        }
            break;

        default:
            break;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor magentaColor];
    // Do any additional setup after loading the view.
      [self.collectionView reloadData];
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    self.navigationItem.title = @"今日";
    [CK_MenuViewController getMenuViewController].menuArray = [@[@" 今日 • TODAY",@" 话题 • TOPICS",@" 电台 • RADIO",@" 良品 • PRODUCTS",@" 阅读 • SUBSCRITION"] mutableCopy];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

//    创建collectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds  collectionViewLayout:layout];
    //    设置layout
    layout.itemSize = CGSizeMake(self.view.frame.size.width *0.332, self.view.frame.size.height *0.213);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 202);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"JaneCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    //    注册头
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

     self.cycleView = [[JaneCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:2];
    
    //初始化数据源
    self.dataSource = [NSMutableArray new];
    [JaneNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] RequestMethod:CK_GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
               if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = jsonDict[@"data"];
            for (NSDictionary *itemDict in dataDict[@"list"]) {
                JaneHomeModel *model = [JaneHomeModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.dataSource addObject:model];
            }
       }
        dispatch_async(dispatch_get_main_queue(), ^{
             [self.collectionView reloadData];
            self.cycleView.imgNameArray = [@[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg"]mutableCopy];
        });
}];

}
#pragma mark----UICollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 创建头轮播图
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview:self.cycleView];
        return header;
    }
    return nil;
}
#pragma mark-----UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JaneHomeDetailController *detailVC = [[JaneHomeDetailController alloc]init];
    JaneHomeModel *model = self.dataSource[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%ld",model.type];
     [self.navigationController pushViewController:detailVC animated:YES];

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JaneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
- (IBAction)didClickLeftBarBtnItem:(UIBarButtonItem *)sender {
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else{
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
